package com.ruoyi.system.controller;

import java.io.IOException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.system.util.MinioUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.AlarmData1;
import com.ruoyi.system.service.IAlarmData1Service;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 报警Controller
 *
 * @author zly
 * @date 2023-07-14
 */
@Api("报警数据")
@RestController
@RequestMapping("/system/alarm")
public class AlarmData1Controller extends BaseController {
    @Autowired
    private IAlarmData1Service alarmData1Service;

    @Autowired
    private ServerConfig serverConfig;
    @Autowired
    private MinioUtil minioUtil;

    /**
     * 查询报警列表
     */
    @ApiOperation("查询报警数据列表")
    @PreAuthorize("@ss.hasPermi('system:alarm:list')")
    @GetMapping("/list")
    public TableDataInfo list(AlarmData1 alarmData1) {
        startPage();
        String handleState = alarmData1.getHandleState();

        String status = alarmData1.getStatus();


        if ("0".equals(handleState) && (handleState !="") &&(handleState !=null)) {
            alarmData1.setHandleState("未处理");

        } else  if ("1".equals(handleState)&& (handleState !="") &&(handleState !=null)){
            alarmData1.setHandleState("已处理");

        }

        if ("0".equals(status) && (status !="") &&(status !=null)) {
            alarmData1.setStatus("未恢复");

        } else  if ("1".equals(status)&& (status !="") &&(status !=null)){
            alarmData1.setStatus("已恢复");

        }

        List<AlarmData1> list = alarmData1Service.selectAlarmData1List(alarmData1);
        return getDataTable(list);
    }

    /**
     * 导出报警列表
     */
    @ApiOperation("导出报警数据列表")
    @PreAuthorize("@ss.hasPermi('system:alarm:export')")
    @Log(title = "报警", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AlarmData1 alarmData1) {
        List<AlarmData1> list = alarmData1Service.selectAlarmData1List(alarmData1);
        ExcelUtil<AlarmData1> util = new ExcelUtil<AlarmData1>(AlarmData1.class);
        util.exportExcel(response, list, "报警数据");
    }

    /**
     * 获取报警详细信息
     */
    @ApiOperation("获取报警信息详情")
    @PreAuthorize("@ss.hasPermi('system:alarm:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(alarmData1Service.selectAlarmData1ById(id));
    }

    //图片路径
    String urlFile=null;

    /**
     * 处理图片上传
     * @param file
     * @return
     * @throws IOException
     */
    @SneakyThrows
    @ApiOperation("接收前端图片")
    @Log(title = "预览缩略图", businessType = BusinessType.UPDATE)
    @PostMapping("/articleImg")
    public String uploadFile(MultipartFile file) throws IOException {
        if (file!=null){
            urlFile=minioUtil.minioUpload(file);
        }
        return urlFile;
    }

    /**
     * 处理报警信息
     */
    @ApiOperation("处理报警信息")
    @PreAuthorize("@ss.hasPermi('system:alarm:edit')")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @PostMapping("/uploadImgUrl")
    public AjaxResult uploadFile(@RequestBody AlarmData1 alarmData)  {
        try{
            AlarmData1 alarmData1=new AlarmData1();
            //获取当前登录人
            Authentication authentication= SecurityContextHolder.getContext().getAuthentication();
            String name = authentication.getName();
            alarmData1.setHandlePeople(name);
            alarmData1.setHandlePicture(urlFile);
            alarmData1.setNotes(alarmData.getNotes());
            alarmData1.setId(alarmData.getId());
            alarmData1.setHandleState("已处理");
            alarmData1.setStatus("已恢复");
            alarmData1Service.updateAlarmData1(alarmData1);
            return success("新增成功");
        } catch (Exception e) {
            return error("新增失败");
        }
    }

    /**
     * 删除报警
     */
    @ApiOperation("删除报警数据")
    @PreAuthorize("@ss.hasPermi('system:alarm:remove')")
    @Log(title = "报警", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(alarmData1Service.deleteAlarmData1ByIds(ids));
    }
}
